﻿/********************************************************************
	文件：BoxShape.cs
    作者：Pangdudu
    日期：2021/03/02
    描述: box盒子的形状
*********************************************************************/

using UnityEngine;

namespace ShapeMesh
{
    public class BoxShape : Shape
    {
        private static readonly string _boxMeshName = "Box Mesh";//mesh的名称

        #region private members
        private readonly float _xSize, _ySize, _zSize;
        #endregion

        #region ctor
        public BoxShape(float xSize, float ySize, float zSize, MeshPivot meshPivot) : base(meshPivot, _boxMeshName)
        {
            _xSize = xSize;
            _ySize = ySize;
            _zSize = zSize;

            _vertexOffset = GetVertexOffset();
        }

        #endregion

        #region override functions
        /// <summary>
        /// 根据mesh中心点的位置，获得顶点位置的偏移量
        /// </summary>
        /// <returns></returns>
        protected sealed override Vector3 GetVertexOffset()
        {
            return _meshPivot switch
            {
                MeshPivot.Center => Vector3.zero,
                MeshPivot.Top => new Vector3(0, _ySize, 0),
                MeshPivot.Bottom => new Vector3(0, -_ySize, 0),
                MeshPivot.Left => new Vector3(-_xSize, 0, 0),
                MeshPivot.Right => new Vector3(_xSize, 0, 0),
                MeshPivot.Front => new Vector3(0, 0, _zSize),
                MeshPivot.Back => new Vector3(0, 0, -_zSize),
                _ => Vector3.zero
            } * 0.5f;
        }

        /// <summary>
        /// 获得顶点的数据集合
        /// </summary>
        /// <returns></returns>
        protected override Vector3[] GetVertices()
        {
            var p0 = new Vector3(-_xSize * 0.5f, -_ySize * 0.5f, -_zSize * 0.5f) - _vertexOffset;//左下
            var p1 = new Vector3(_xSize * 0.5f, -_ySize * 0.5f, -_zSize * 0.5f) - _vertexOffset;//右下
            var p2 = new Vector3(_xSize * 0.5f, -_ySize * 0.5f, _zSize * 0.5f) - _vertexOffset;//右上
            var p3 = new Vector3(-_xSize * 0.5f, -_ySize * 0.5f, _zSize * 0.5f) - _vertexOffset;//左上

            var p4 = new Vector3(-_xSize * 0.5f, _ySize * 0.5f, -_zSize * 0.5f) - _vertexOffset;//左下
            var p5 = new Vector3(-_xSize * 0.5f, _ySize * 0.5f, _zSize * 0.5f) - _vertexOffset;//左上
            var p6 = new Vector3(_xSize * 0.5f, _ySize * 0.5f, _zSize * 0.5f) - _vertexOffset;//右上
            var p7 = new Vector3(_xSize * 0.5f, _ySize * 0.5f, -_zSize * 0.5f) - _vertexOffset;//右下

            return new []
             {
                //下
                p1,p2,p3,p0,
                //上
                p4,p5,p6,p7,
                //左
                p3,p5,p4,p0,
                //右
                p1,p7,p6,p2,
                //前
                p2,p6,p5,p3,
                //后
                p0,p4,p7,p1
             };
        }

        /// <summary>
        /// 获得法线方向的数据集合
        /// </summary>
        /// <returns></returns>
        protected override Vector3[] GetNormals()
        {
            return new Vector3[]
            {
                //下
                Vector3.down,Vector3.down,Vector3.down,Vector3.down,
                //上
                Vector3.up, Vector3.up, Vector3.up, Vector3.up,
                //左
                Vector3.left,Vector3.left,Vector3.left,Vector3.left,
                //右
                Vector3.right,Vector3.right,Vector3.right,Vector3.right,
                //前
                Vector3.forward, Vector3.forward, Vector3.forward, Vector3.forward,
                //后
                Vector3.back,Vector3.back,Vector3.back,Vector3.back,
            };
        }

        /// <summary>
        /// 获得三角面顶点的索引
        /// </summary>
        /// <returns></returns>
        protected override int[] GetTriangles()
        {
            return new int[]
            {
                //下
                0, 1, 2,
                2, 3, 0,
                //上
                0 + 4 * 1, 1 + 4 * 1, 2 + 4 * 1,
                2 + 4 * 1, 3 + 4 * 1, 0 + 4 * 1,
                //左
                0 + 4 * 2, 1 + 4 * 2, 2 + 4 * 2,
                2 + 4 * 2, 3 + 4 * 2, 0 + 4 * 2,
                //右
                0 + 4 * 3, 1 + 4 * 3, 2 + 4 * 3,
                2 + 4 * 3, 3 + 4 * 3, 0 + 4 * 3,
                //前
                0 + 4 * 4, 1 + 4 * 4, 2 + 4 * 4,
                2 + 4 * 4, 3 + 4 * 4, 0 + 4 * 4,
                //后
                0 + 4 * 5, 1 + 4 * 5, 2 + 4 * 5,
                2 + 4 * 5, 3 + 4 * 5, 0 + 4 * 5,
            };
        }

        /// <summary>
        ///  获得UV坐标的数据集合
        /// </summary>
        /// <returns></returns>
        protected override Vector2[] GetUVs()
        {
            return new Vector2[]
             {
                new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
                new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
                new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
                new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
                new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
                new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
             };
        }

        #endregion
    }
}